package com.geek.jdbc.pool.custom;

import com.geek.jdbc.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Desc TODO
 * @Author
 * @Date 2025/11/17
 */
public class PoolTest {

    public static void main(String[] args) throws SQLException {
        MyDataSource pool = new MyDataSource();

        Connection conn1 = pool.getConnection();
        //com.mysql.cj.jdbc.ConnectionImpl@4d1b0d2a
        System.out.println("conn1 = "+conn1);
        Connection conn2 = pool.getConnection();
        System.out.println("conn2 = "+conn2);
        Connection conn3 = pool.getConnection();
        System.out.println("conn3 = "+conn3);
        Connection conn4 = pool.getConnection();
        System.out.println("conn4 = "+conn4);
        Connection conn5 = pool.getConnection();
        System.out.println("conn5 = "+conn5);

        //查看ID为1的女生信息
        PreparedStatement ppst = conn1.prepareStatement("select name from girl where id=?");
        ppst.setInt(1, 1);
        ResultSet rs = ppst.executeQuery();
        while (rs.next()){
            String name = rs.getString(1);
            System.out.println("ID为1的女生信息 = "+name);
        }

        //释放资源
        JDBCUtils.close(ppst , null , rs);

        //归还连接至连接池中
        pool.close(conn1);

        //再次尝试获取连接对象
        Connection conn6 = pool.getConnection();
        System.out.println("conn6 = "+conn6);
    }

}
